//
//  ViewController.m
//  TestUrlDemo
//
//  Created by huangwei on 16/6/30.
//  Copyright © 2016年 天翼阅读. All rights reserved.
//
#define SCREEN_WIDTH   CGRectGetWidth([UIScreen mainScreen].bounds)

#define SCREEN_HEIGHT  CGRectGetHeight([UIScreen mainScreen].bounds)

#define LEFT_SPACE  20

#define TOP_SPACE   200
#import "ViewController.h"
#import "WebViewVC.h"
@interface ViewController ()<UITextFieldDelegate>{

    UITextField *_textField;

}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"主界面";
    self.view.backgroundColor=[UIColor grayColor];
    
    UILabel *textLabel=[[UILabel alloc]initWithFrame:CGRectMake(LEFT_SPACE, TOP_SPACE, SCREEN_WIDTH-LEFT_SPACE*2, 30)];
    textLabel.text=@"请输入有效的URL地址";
    textLabel.textAlignment=NSTextAlignmentCenter;
    textLabel.backgroundColor=[UIColor clearColor];
    textLabel.font=[UIFont systemFontOfSize:18];
    [self.view addSubview:textLabel];
    
    _textField=[[UITextField alloc]initWithFrame:CGRectMake(LEFT_SPACE, CGRectGetMaxY(textLabel.frame)+10,SCREEN_WIDTH-LEFT_SPACE*2 , 50)];
    _textField.delegate=self;
    _textField.backgroundColor=[UIColor whiteColor];
    _textField.layer.borderWidth=0.5;
    _textField.layer.borderColor=[UIColor blueColor].CGColor;
    _textField.layer.cornerRadius=3;
    [self.view addSubview:_textField];
    
    UIButton *submitBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.frame=CGRectMake((SCREEN_WIDTH-60)/2, CGRectGetMaxY(_textField.frame)+30, 60, 30);
    submitBtn.backgroundColor=[UIColor blueColor];
    [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    submitBtn.layer.borderWidth=1;
    submitBtn.layer.borderColor=[UIColor redColor].CGColor;
    submitBtn.layer.cornerRadius=3;
    [submitBtn addTarget:self action:@selector(jumpToWebView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    
    
}
-(void)jumpToWebView:(UIButton*)sender{

    BOOL loadWebViewBySystemBrowser=NO;
    
    NSString *str=[_textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (str.length>0) {
        
        NSString *urlStr;
        
        if ([[str lowercaseString] containsString:@"http://"]) {
            urlStr=str;
        } else {
           urlStr=[NSString stringWithFormat:@"http://%@",str];
        }
       
        if (loadWebViewBySystemBrowser) {
            
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr]];
            
        }else{
        
            WebViewVC *web=[[WebViewVC alloc]initWithUrlString:urlStr];
            [self.navigationController pushViewController:web animated:YES];
        }
        
    }
    


}
-(void)viewWillDisappear:(BOOL)animated{
    
     [_textField endEditing:YES];
    NSLog(@"新增加一条输出语句");
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField endEditing:YES];
    return YES;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
